<?php

/**
 * Paging
 */
class Zend_View_Helper_Pagination extends Zend_View_Helper_Abstract {
    
    /**
    * Pagination
    * @param <array> $arrParams
    * @return string
    * @author : KIETTHM
    */
    public function Pagination($arrParams = array(),$intPerpage)
    {
        $strReturn = '';
        
        if($arrParams['total'] > $intPerpage)
        {
            //Init Check Param
            $intTotalRecord = $arrParams['total'];
            $record_page = $arrParams['perpage'];
            $intPage = $arrParams['page'];
            $dot = isset($arrParams['separate'])?$arrParams['separate']:'-';
            $strLink = $arrParams['uri'];
            $intItem = isset($arrParams['showItem'])?$arrParams['showItem']:3;
            $idPaging = isset($arrParams['idPagination'])?$arrParams['idPagination']:'pagination';
            $extraEnd = isset($arrParams['extEnd'])?$arrParams['extEnd']:'.html';

            //Get Data Page
            $intTotalPage = ceil($intTotalRecord / $record_page);
            $intStart = (($intPage-1)*$record_page + 1);
            $intEnd = min($intPage*$record_page, $intTotalRecord);
            $strUri = $dot;
            $intPage = max($intPage, 1);
            $intPage = min($intTotalPage, $intPage);

            $intPageStart = ($intPage - $intItem > 0) ? ($intPage - $intItem) : 1;
            $intPageEnd = ($intTotalPage - ($intPage + $intItem) > 0) ? ($intPage + $intItem) : $intTotalPage;
            $strReturn = '<div id="'.$idPaging.'"><ul>';
            $strtPreviousPageLink = ($intPage > 1) ? $strLink . $strUri . ($intPage - 1).$extraEnd : 'javascript:void(0)';
            if($intPage > 1)
            {
                $strReturn .='<li class="pading_btn"><a href="'.$strtPreviousPageLink.'" class="next">&nbsp;</a></li>';
            }
            if(($intPage-$intItem)>2)
            {
                $strLinkPage1 = $strLink . $strUri . '1'.$extraEnd;
                $strReturn .= '<li '.$strTempClass.'><a href="'.$strLinkPage1.'"><span>1</span></a></li>';
                $strReturn .= '<li '.$strTempClass.'>...</li>';
            }


            for ($intTemp = $intPageStart; $intTemp <= $intPageEnd; $intTemp ++) {
                $strTempLink = ($intTemp == $intPage) ? 'javascript:void(0)' : $strLink . $strUri . $intTemp.$extraEnd;
                $strTempClass = ($intTemp == $intPage) ? ' class="active"':'';

                $strReturn .= '<li '.$strTempClass.'><a href="'.$strTempLink.'"><span>'.$intTemp.'</span></a></li>';
            }

            if(($intPage+$intItem)<($intTotalPage-1))
            {
                $strLinkPageEnd = $strLink . $strUri . $intTotalPage .$extraEnd;
            }

            $strtNextPageLink = ($intTotalPage > $intPage) ? $strLink . $strUri . ($intPage + 1).$extraEnd : 'javascript:void(0)';


            if($intTotalPage > $intPage)
            {
                $strReturn .='<li class="pading_btn"><a href="'.$strtNextPageLink.'" class="prve">&nbsp;</a></li>';
            }
            $strReturn .= '</ul></div>';
        }

        //return Data
        return $strReturn;
    }

}